#import "layer_menu_option.h"

@implementation  LayerMenuOption

- (id)initLayerMenuOption
{
	CCSprite *_Background;
	NSString *_Path;
	CCSprite *_LabelBgm;
	CCSprite *_LabelSoundex;
	CCMenuItemImage *_MenuItemBgm;
	CCMenuItemImage *_MenuItemSoundex;
	CCMenuItemImage *_MenuItemBack;

	if ((self = [super init]))
	{
		_Background = uiCreateImageObject(&g_CfgMenuOptionImageBackground, self);
		_LabelBgm = uiCreateImageObject(&g_CfgMenuOptionImageBgm, self);
		_LabelSoundex = uiCreateImageObject(&g_CfgMenuOptionImageSoundex, self);

		if (g_OptionBgm)
		{
			_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOnPath];
		}
		else
		{
			_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOffPath];
		}
		itemBgm_ = [CCSprite spriteWithSpriteFrameName:_Path];

		if (g_OptionSoundex)
		{
			_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOnPath];
		}
		else
		{
			_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOffPath];
		}
		itemSoundex_ = [CCSprite spriteWithSpriteFrameName:_Path];

		_MenuItemBgm = [CCMenuItemImage itemFromNormalSprite:itemBgm_ selectedSprite:nil target:self selector:@selector(CallBackMenuItemBgm:)];
		_MenuItemSoundex = [CCMenuItemImage itemFromNormalSprite:itemSoundex_ selectedSprite:nil target:self selector:@selector(CallBackMenuItemSoundex:)];
		_MenuItemBack = uiCreateMenuItemImageObject(&g_CfgMenuOptionItemBack, self, @selector(CallBackMenuItemBack:));
		_MenuItemBgm.positionInPixels = g_CfgMenuOptionItemBgmPosition;
		_MenuItemSoundex.positionInPixels = g_CfgMenuOptionItemSoundexPosition;
		uiNodeAddMenu(self, CGPointZero, _MenuItemBgm, _MenuItemSoundex, _MenuItemBack, nil);

		g_LayerMenuMain.flagLockFunction = true;
	}
	return self;
}

- (void)dealloc
{
	[g_LayerMenuMain resumeSchedulerAndActions];

	[super dealloc];
}

- (void)draw
{
	UICenterRect _Rect;

	_Rect.center = g_ScrrenCenter;
	_Rect.size = g_ScrrenSize;
	glColor4f(0.0f, 0.0f, 0.0f, 0.7f);
	uiRenderRect(_Rect);
}

- (void)CallBackMenuItemBgm:(id)Sender
{
	NSString *_Path;

	/*effect
	*/
	EffectButton();

	g_OptionBgm = !g_OptionBgm;
	if (g_OptionBgm)
	{
		_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOnPath];
		MusicPlayMain();
	}
	else
	{
		_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOffPath];
		MusicStop();
	}
	[itemBgm_ setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_Path]];
}

- (void)CallBackMenuItemSoundex:(id)Sender
{
	NSString *_Path;

	g_OptionSoundex = !g_OptionSoundex;
	if (g_OptionSoundex)
	{
		_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOnPath];
	}
	else
	{
		_Path = [NSString stringWithUTF8String:g_CfgMenuOptionOffPath];
	}
	[itemSoundex_ setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_Path]];

	/*effect
	*/
	EffectButton();
}

- (void)CallBackMenuItemBack:(id)Sender
{
	/*effect
	*/
	EffectButton();

	[g_SceneMenu removeChild:g_LayerMenuOption cleanup:TRUE];
	g_LayerMenuOption = nil;
	g_LayerMenuMain.flagLockFunction = false;
}

@end